using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class OperationMachineItem : BaseItem
	{
		private float MIN_MACHINE_Y = -2.4f;

		private float MAX_MACHINE_Y = -0.67f;

		private float MIN_POLE_X;

		private float MAX_POLE_X = 1f;

		private Transform machine;

		private Vector2 machineOP;

		private Transform pole1;

		private Vector2 pole1OP;

		private Transform pole2;

		private bool isClickMachine;

		private Vector2 lastTouchPoint;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform trans = model.Find("Mask");
			trans.SetLocalZ(-1E-05f);
			pole1 = model.Find("Pole1");
			pole1OP = pole1.localPosition;
			pole2 = pole1.Find("Pole2");
			pole2.SetLocalZ(-1E-05f);
			machine = pole2.Find("Machine");
			machine.SetLocalZ(-1E-05f);
			machine.gameObject.layer = 15;
			machineOP = machine.localPosition;
			if (data.state != 0)
			{
				SetPole1Position(data.modelX);
				SetMachinePosition(data.modelY);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			isClickMachine = false;
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null && raycastHit2D.collider.transform == machine)
			{
				isClickMachine = true;
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			SetMachinePosition(machine.localPosition.y + touchEventData.curPos.y - lastTouchPoint.y);
			SetPole1Position(pole1.localPosition.x + touchEventData.curPos.x - lastTouchPoint.x);
			lastTouchPoint = touchEventData.curPos;
		}

		private void SetMachinePosition(float val)
		{
			machine.SetLocalY(Mathf.Clamp(val, MIN_MACHINE_Y, MAX_MACHINE_Y));
		}

		private void SetPole1Position(float val)
		{
			pole1.SetLocalX(Mathf.Clamp(val, MIN_POLE_X, MAX_POLE_X));
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = 1;
			obj["34"] = pole1.localPosition.x;
			obj["39"] = machine.localPosition.y;
		}
	}
}
